#include <my_head.h>
#include "client.h"

char addrbuff[128] = {0};
char portbuff[5] = {0};
char serch_name[128] = {0};
int root = 0; // 是否是root用户

void do_link_server();               // 连接服务器
int do_login(int s_fd, msg_t *msg);  // 登录
void login_ok(int s_fd, msg_t *msg); // 登录成功
void msginfo_print(msg_t *msg);      // 打印查询到的消息

void do_query_user(int s_fd, int nbytes, msg_t *msg);  // 查询用户
void do_insert_user(int s_fd, int nbytes, msg_t *msg); // 添加用户
void do_alter_user(int s_fd, int nbytes, msg_t *msg);  // 更改用户
void do_delete_user(int s_fd, int nbytes, msg_t *msg); // 删除用户
void send_rev(int s_fd, int nbytes, msg_t *msg);       // 发送接收

void link_print(int *choose)
{   
    printf("\n");
    printf("******欢迎使用员工管理系统******\n");
    printf("********************************\n");
    printf("* 1: 联网  2: 退出 *\n");
    printf("********************************\n");
    printf("请选择 choose : ");
    scanf("%d", choose);
}
void login_print(int *choose)
{
    printf("\n");
    printf("********************************\n");
    printf("* 1:登录  2: 退出 *\n");
    printf("********************************\n");
    printf("请选择 choose : ");
    scanf("%d", choose);
}
void intput_addr_port()
{
    printf("\n");
    printf("********************************\n");
    printf("请输入ip地址>>>:");
    scanf("%s", addrbuff);

    printf("请输端口号>>>:");
    scanf("%s", portbuff);
}
void root_print(int *choose)
{   
    printf("\n");
    printf("***********请选择您的操作**********\n");
    printf("***********************************\n");
    printf("* 1: 查  2: 增  3: 删  4: 改  5: 退出*\n");
    printf("***********************************\n");
    printf("请选择 choose : ");
    scanf("%d", choose);
}

void n_root_print(int *choose)
{
    printf("\n");
    printf("************请选择您的操作***********\n");
    printf("***********************************\n");
    printf("* 1: 查  4: 改  5: 退出*\n");
    printf("***********************************\n");
    printf("请选择 choose : ");
    scanf("%d", choose);
}

int main(int argc, const char *argv[])
{

    int choose;
    while (1)
    {   
        link_print(&choose);
        switch (choose)
        {
        case 1:
            intput_addr_port();
            do_link_server();
            break;
        case 2:
            return 0;
        default:
            break;
        }
    }
    return 0;
}
void do_link_server()
{
    // 创建socket
    int s_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (s_fd < 0)
    {
        ERR_LOG("socket error");
    }

    struct sockaddr_in saddr;
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = inet_addr(addrbuff);
    saddr.sin_port = htons(atoi(portbuff));

    printf("连接中...\n");
    if (connect(s_fd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1)
    {
        printf("连接失败\n");
        close(s_fd);
        exit(-1);
    }
    printf("连接成功\n");

    msg_t msg;
    int choose;
    while (1)
    {
        login_print(&choose);
        switch (choose)
        {
        case 1:
            if (do_login(s_fd, &msg) == 0)
            {
                login_ok(s_fd, &msg);
            }
            break;
        case 2:
            close(s_fd);
            printf("退出成功");
            exit(-1);
        default:
            break;
        }
    }
}
// 登录
int do_login(int s_fd, msg_t *msg)
{
    msg->commd = '0';
    printf("\n");
    printf("请输入用户名>>>:");
    scanf("%s", msg->name);
    printf("请输入登录密码>>>:");
    scanf("%s", msg->password);

    // 先发送
    send(s_fd, msg, sizeof(msg_t), 0);
    // 再接收
    recv(s_fd, msg, sizeof(msg_t), 0);
    if (strcmp(msg->tip, "登录成功") != 0)
    {
        printf("登录失败：用户名或密码错误～\n");
        return -1;
    }
    
    // 登录成功后保存是否是root用户
    root = msg->root;
    root == 1 ? printf("%s:管理员\n", msg->tip) : printf("%s:普通用户\n", msg->tip);
    return 0;  
}
// 登录成功
void login_ok(int s_fd, msg_t *msg)
{
    int choose;
    int nbytes = 0;
    while (1)
    {   
        if (root == 1)
        {
            root_print(&choose);
        }
        else
        {
            n_root_print(&choose);
        }
        switch (choose)
        {
        case 1: // 查
            do_query_user(s_fd, nbytes, msg);
            break;
        case 2: // 增
            do_insert_user(s_fd, nbytes, msg);
            break;
        case 3: // 删
            do_delete_user(s_fd, nbytes, msg);
            break;
        case 4: // 改
            do_alter_user(s_fd, nbytes, msg);
            break;
        case 5: // 退
            close(s_fd);
            exit(-1);
        default:
            break;
        }
    }
}
// 查询信息
void do_query_user(int s_fd, int nbytes, msg_t *msg)
{
    if (root == 1)
    {
        int choose;
        printf("***********************************\n");
        printf("* 0:名字  1: 全部\n");
        printf("***********************************\n");
        printf("请输入您要查询的类型>>>:");
        scanf("%d", &choose);
        memset(serch_name, 0, sizeof(serch_name));
        if (choose == 0)
        {
            printf("请输入您要查询的用户名>>>:");
            scanf("%s", serch_name);
            msg->search_type = 0;
            strcpy(msg->name, serch_name);
        }else if (choose == 1)//全部
        {
            msg->search_type = 1;
        }else{
            return;
        }
    }else{
        msg->search_type = 0;
    }
    msg->commd = '4';

    send(s_fd, msg, sizeof(msg_t), 0);

    int count = 0;
    int i = 0;
    do
    {
        memset(msg, 0, sizeof(msg_t));
        if (-1 == (nbytes = recv(s_fd, msg, sizeof(msg_t), 0)))
        {
            ERR_LOG("recv error");
        }
        if (nbytes == 0)
        {
            printf("服务器连接关闭\n");
            close(s_fd);
            exit(-1);
        }
        if (msg->search_count == 0)
        {   
            //printf("此用户不存在\n");
            printf("%s\n", msg->tip);
        }else{
            msginfo_print(msg);
            count = msg->search_count;
        }
        i++;
        
    } while (i < count);
}
// 打印查询到的消息
void msginfo_print(msg_t *msg)
{
    printf("%-10s\t%d\t%s\t%s\t%s\t%d\t%s\t%s\n", msg->name, msg->age, msg->phone, msg->sex, msg->position, msg->salary, msg->address, msg->password);
}
// 添加用户
void do_insert_user(int s_fd, int nbytes, msg_t *msg)
{
    char name[128];
    int age;
    char phone[12];
    char sex[5];
    char position[128];
    int salary;
    char address[256];
    char password[7];
    printf("请输入用户名>>>:");
    scanf("%s", name);
    printf("请输入用户年龄>>>:");
    scanf("%d", &age);
    printf("请输入用户手机号>>>:");
    scanf("%s", phone);
    printf("请输入用户性别>>>:");
    scanf("%s", sex);
    printf("请输入用户职位>>>:");
    scanf("%s", position);
    printf("请输入用户薪资>>>:");
    scanf("%d", &salary);
    printf("请输入用户地址>>>:");
    scanf("%s", address);
    printf("请设置用户密码>>>:");
    scanf("%s", password);

    strcpy(msg->name, name);
    msg->age = age;
    strcpy(msg->phone, phone);
    strcpy(msg->sex, sex);
    strcpy(msg->position, position);
    msg->salary = salary;
    strcpy(msg->address, address);
    strcpy(msg->password, password);
    msg->root = 0;

    msg->commd = '1';
    send_rev(s_fd, nbytes, msg);
    printf("%s\n", msg->tip);
    msginfo_print(msg);
}
// 删除用户
void do_delete_user(int s_fd, int nbytes, msg_t *msg)
{
    printf("请输入您要删除的用户名>>>:");
    scanf("%s", serch_name);
    printf("您确定要删除此用户吗?(yes/no)>>>:");
    char ch[4] = {0};
    scanf("%s", ch);
    if (strcmp(ch, "yes") != 0)
    {
        return;
    }
    strcpy(msg->name, serch_name);
    msg->commd = '2';

    send_rev(s_fd, nbytes, msg);
    printf("%s\n", msg->tip);
}
// 更改信息
void do_alter_user(int s_fd, int nbytes, msg_t *msg)
{
    int age;
    char phone[12];
    char position[128];
    int salary;
    char address[256];
    printf("请输入修改后的年龄>>>:");
    scanf("%d", &age);
    printf("请输入修改后的手机号>>>:");
    scanf("%s", phone);
    printf("请输入修改后的职位>>>:");
    scanf("%s", position);
    printf("请输入修改后的薪资>>>:");
    scanf("%d", &salary);
    printf("请输入修改后的地址>>>:");
    scanf("%s", address);

    msg->age = age;
    strcpy(msg->phone, phone);
    strcpy(msg->position, position);
    msg->salary = salary;
    strcpy(msg->address, address);

    msg->commd = '3';
    send_rev(s_fd, nbytes, msg);
    printf("%s\n", msg->tip);
    msginfo_print(msg);
}
// 发送接收
void send_rev(int s_fd, int nbytes, msg_t *msg)
{
    send(s_fd, msg, sizeof(msg_t), 0);
    memset(msg, 0, sizeof(msg_t));

    if (-1 == (nbytes = recv(s_fd, msg, sizeof(msg_t), 0)))
    {
        ERR_LOG("recv error");
    }
    if (nbytes == 0)
    {
        printf("服务器连接关闭\n");
        close(s_fd);
        exit(-1);
    }
}